﻿using AiXiu.Common;
using AiXiu.IService;
using AiXiu.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AiXiu.Service
{
    public class VideoService : IVideoService
    {
        Model1 model = new Model1();
        /// <summary>
        /// 数据库添加视频
        /// </summary>
        /// <param name="video"></param>
        /// <returns></returns>
        public TBVideos AddVideo(TBVideos video)
        {
            video.Status = (int)VideoStatus.Uploading;
            video.UploadTime = DateTime.Now;
            model.Entry(video).State = EntityState.Added;
            model.SaveChanges();
            return video;
        }

        /// <summary>
        /// 通过用户id获取视频列表
        /// </summary>
        /// <param name="userId">用户id</param>
        /// <returns></returns>
        public List<TBVideos> GetVideosByUserId(int userId)
        {
            List<TBVideos> list = model.TBVideos.Where(a => a.UserId == userId).OrderByDescending(a => a.UploadTime).ToList();
            return list;
        }

        /// <summary>
        /// 批量更新视频信息
        /// </summary>
        /// <returns>返回更新数量</returns>
        public int UpdateVideos()
        {
            //1、获取处理中的视频Id列表（上传中、上传成功，转码中）
            List<TBVideos> videoList = model.TBVideos.Where(a => a.Status == (int)VideoStatus.Uploading || a.Status == (int)VideoStatus.UploadSucces || a.Status == (int)VideoStatus.Transcoding).ToList();

            //2、阿里云平台查询这些视频的信息——状态、封面
            List<string> videoIds = videoList.Select(a => a.VideoId).ToList();
            if (videoIds.Count > 0)
            {
                //	调用VodHelper中【批量获取视频信息_阿里云平台的视频信息】的方法，获取视频信息结果类GetVideoInfosResult
                VodHelper vodHelper = new VodHelper();
                GetVideoInfosResult result = vodHelper.GetVideoInfos(videoIds);

                //3、视频的状态和封面更新到数据库
                List<VideoInfo> list = result.VideoList;
                foreach (VideoInfo videoInfo in list)
                {
                    TBVideos video = model.TBVideos.Find(videoInfo.VideoId);
                    video.Status = (int)videoInfo.Status;
                    if (!string.IsNullOrEmpty(videoInfo.CoverURL))
                    {
                        int lastIndex = videoInfo.CoverURL.IndexOf('?');
                        video.CoverURL = videoInfo.CoverURL.Substring(0, lastIndex);
                    }
                    model.Entry(video).State = EntityState.Modified;
                    model.SaveChanges();
                }
                return videoIds.Count;
            }
            else
            {
                return 0;
            }

           
        }

        /// <summary>
        /// 根据视频id删除视频
        /// </summary>
        /// <param name="videoId"></param>
        public void DeleteVideo(string videoId)
        {
            //从数据删除视频
            TBVideos video = model.TBVideos.Find(videoId);
            model.TBVideos.Remove(video);
            //dbContext.Entry(video).State = EntityState.Deleted;
            model.SaveChanges();

            //从阿里云删除视频
            VodHelper vodHelper = new VodHelper();
            List<string> videoIds = new List<string>();
            videoIds.Add(videoId);
            vodHelper.DeleteVideos(videoIds);
        }

        /// <summary>
        /// 获取视频列表
        /// </summary>
        /// <returns></returns>
        public List<TBVideos> GetVideos()
        {
            IVideoService videoService = new VideoService();
            int status = (int)VideoStatus.Normal;
            List<TBVideos> list= model.TBVideos
                .Where(m => m.Status == status)
                .OrderByDescending(m => m.UploadTime)
                .ToList();
            return list;
        }

        /// <summary>
        /// 通过视频Id获取视频信息
        /// </summary>
        /// <param name="videoId"></param>
        /// <returns></returns>
        public TBVideos GetVideoByVideoId(string videoId)
        {
            TBVideos video = model.TBVideos.Find(videoId);
            return video;
        }

    }
}
